using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace HelloXNA.Component.Sprite
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class AnimatingSprite : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch _spriteBatch;

        Texture2D _sprite;

        Point _frameSize = new Point(128, 128);

        Point _currentFrame = new Point(0, 0);

        Point _sheetSize = new Point(4, 4);

        const int SPRITE_FPS = 10;

        int _elapsedTime = 0;

        
        public AnimatingSprite(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            _sprite = Game.Content.Load<Texture2D>("SpriteSheet/Smurf");
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            _elapsedTime += gameTime.ElapsedGameTime.Milliseconds;

            if (_elapsedTime > 1000 / SPRITE_FPS)
            {
                _elapsedTime -= 1000 / SPRITE_FPS;
                ++_currentFrame.X;
                if (_currentFrame.X >= _sheetSize.X)
                {
                    _currentFrame.X = 0;
                    ++_currentFrame.Y;
                    if (_currentFrame.Y <= _sheetSize.Y)
                    {
                        _currentFrame.Y = 0;
                    }
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Game.GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();

            _spriteBatch.Draw(_sprite, new Vector2((Game.Window.ClientBounds.Width - _frameSize.X) / 2, (Game.Window.ClientBounds.Height - _frameSize.Y) / 2),
                new Rectangle(_currentFrame.X * _frameSize.X, _currentFrame.Y * _frameSize.Y, _frameSize.X, _frameSize.Y), Color.White);

            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
